/**
 * Created by bootdo.
 */
import axios from 'axios'
import Cookies from 'js-cookie'
import Bus from '../bus'

axios.defaults.withCredentials = false;
axios.defaults.timeout = 180000;
axios.defaults.headers.post['Content-Type'] = 'application/json';

// axios.defaults.headers.common['Authorization'] = AUTH_TOKEN;
// axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded;charset=UTF-8';//配置请求头

//添加一个请求拦截器
axios.interceptors.request.use(
  config => {
    if(localStorage.getItem("exportFlag") == '1'){
      if(Cookies.get('token1')){
        config.headers.Authorization = Cookies.get('token1');
      }else{
        Bus.$emit('dataLogin', '/dataLogin');
      }
    }else{
      //请求omap服务的请求头必须携带Authorization
      if(Cookies.get('token')){
        config.headers.Authorization = Cookies.get('token');
      }else{
        //登陆跳转
        Bus.$emit('login', '/login');
      }
    }
    return config;
  },
  error => {
    return Promise.reject(error)
  }
);
// 添加一个响应拦截器
axios.interceptors.response.use(function (response) {
  //未登录
  if (parseInt(response.status) === 401 || parseInt(response.status) === 403) {
    if(localStorage.getItem("exportFlag") == '1'){
      Bus.$emit('dataLogin', '/dataLogin');
    }else{
      Bus.$emit('login', '/login')
    }
  }
  if (response.data && response.data.code) {
    if (parseInt(response.data.code) === 401 || parseInt(response.data.code) === 403) {
      if(localStorage.getItem("exportFlag") == '1'){
        Bus.$emit('dataLogin', '/dataLogin');
      }else {
        //未登录
        Bus.$emit('login', '/login')
      }
    }
  }
  return response;
}, function (error) {
  // Do something with response error
  return Promise.reject(error);
});


//通用方法
export const POST = (url, params) => {
  return axios.post(`${url}`, params).then(res => res.data)
};
export const POSTHEADER = (url, params,headers) => {
  return axios.post(`${url}`, params,{headers:headers}).then(res => res)
};

export const GET = (url, params) => {
  return axios.get(`${url}`, {
    params: params
  }).then(res => res.data)
};
export const POSTBLOB = (url, params) => {
  return axios({
    method:'post',
    url:`${url}`,
    data:params,
    responseType:'blob'
  }).then(res => res.data)
};
export const GETBLOB = (url, params) => {
  return axios.get(`${url}`, {
    params: params,
    responseType:'blob'
  }).then(res => res.data)
};
export const GETBLOBAll = (url, params) => {
  return axios.get(`${url}`, {
    params: params,
    responseType:'blob'
  }).then(res => res)
};
export const POSTBLOBAll = (url, params) => {
  return axios({
    method:'post',
    url:`${url}`,
    data:params,
    responseType:'blob'
  }).then(res => res)
};
export const GET2 = (url, params) => {
  return axios.get(`${url}`, {
    params: params
  }).then(res => res.data)
};

export const PUT = (url, params) => {
  return axios.put(`${url}`, params).then(res => res.data)
};

export const DELETE = (url, params) => {
  return axios.delete(`${url}`, {
    params: params
  }).then(res => res.data)
};

export const PATCH = (url, params) => {
  return axios.patch(`${url}`, params).then(res => res.data)
};
